/**
 * 搜索相关 Mock 接口
 */

import Mock from 'mockjs'
import type { MockConfig } from '../server'

const searchMocks: MockConfig[] = [
  // 搜索歌曲
  {
    url: '/api/search',
    method: 'GET',
    response: (options: any) => {
      const url = new URL(options.url, 'http://localhost')
      const keyword = url.searchParams.get('keyword') || ''
      const type = url.searchParams.get('type') || 'song'
      const page = parseInt(url.searchParams.get('page') || '1')
      const limit = parseInt(url.searchParams.get('limit') || '20')

      // 根据搜索类型返回不同数据
      switch (type) {
        case 'song':
          return {
            code: 200,
            msg: '搜索成功',
            data: {
              keyword,
              type,
              page,
              limit,
              total: Mock.Random.integer(50, 500),
              results: Mock.mock({
                [`list|${Math.min(limit, 20)}`]: [{
                  'id|+1': 1,
                  name: `${keyword}@ctitle(1, 4)`,
                  artist: '@cname',
                  album: '@ctitle(3, 8)',
                  duration: '@integer(180, 300)',
                  coverUrl: "@image('200x200', '@color', '#FFF', 'Song')",
                  playUrl: 'https://music.example.com/play/@integer(1000, 9999).mp3',
                  'playCount|1000-100000': 1,
                  'matchScore|0.5-1': 1
                }]
              }).list
            }
          }

        case 'artist':
          return {
            code: 200,
            msg: '搜索成功',
            data: {
              keyword,
              type,
              page,
              limit,
              total: Mock.Random.integer(10, 100),
              results: Mock.mock({
                [`list|${Math.min(limit, 15)}`]: [{
                  'id|+1': 1,
                  name: `${keyword}@cname`,
                  avatar: "@image('200x200', '@color', '#FFF', 'Artist')",
                  'albumCount|5-50': 1,
                  'songCount|20-200': 1,
                  'fanCount|1000-1000000': 1,
                  description: '@csentence(20, 50)',
                  'isFollowed|1': false
                }]
              }).list
            }
          }

        case 'album':
          return {
            code: 200,
            msg: '搜索成功',
            data: {
              keyword,
              type,
              page,
              limit,
              total: Mock.Random.integer(20, 200),
              results: Mock.mock({
                [`list|${Math.min(limit, 15)}`]: [{
                  'id|+1': 1,
                  name: `${keyword}@ctitle(2, 8)`,
                  artist: '@cname',
                  coverUrl: "@image('200x200', '@color', '#FFF', 'Album')",
                  'trackCount|8-20': 1,
                  releaseDate: '@date("yyyy-MM-dd")',
                  'playCount|1000-500000': 1,
                  description: '@csentence(10, 30)'
                }]
              }).list
            }
          }

        case 'playlist':
          return {
            code: 200,
            msg: '搜索成功',
            data: {
              keyword,
              type,
              page,
              limit,
              total: Mock.Random.integer(30, 300),
              results: Mock.mock({
                [`list|${Math.min(limit, 15)}`]: [{
                  'id|+1': 1,
                  name: `${keyword}@ctitle(3, 10)`,
                  description: '@csentence(15, 40)',
                  coverUrl: "@image('200x200', '@color', '#FFF', 'Playlist')",
                  'trackCount|10-100': 1,
                  'playCount|1000-1000000': 1,
                  'subscriberCount|100-50000': 1,
                  creator: {
                    id: '@integer(1, 1000)',
                    nickname: '@cname',
                    avatar: "@image('50x50', '@color', '#FFF', 'User')"
                  },
                  updateTime: '@datetime("yyyy-MM-dd HH:mm:ss")'
                }]
              }).list
            }
          }

        default:
          return {
            code: 400,
            msg: '不支持的搜索类型',
            data: null
          }
      }
    }
  },

  // 获取热门搜索词
  {
    url: '/api/search/hot',
    method: 'GET',
    response: () => {
      return {
        code: 200,
        msg: '获取成功',
        data: Mock.mock({
          'list|10-20': [{
            'id|+1': 1,
            keyword: '@ctitle(2, 6)',
            'searchCount|1000-100000': 1,
            'rank|+1': 1,
            'isNew|1': false,
            'isHot|1': true
          }]
        }).list
      }
    }
  },

  // 搜索建议
  {
    url: '/api/search/suggest',
    method: 'GET',
    response: (options: any) => {
      const url = new URL(options.url, 'http://localhost')
      const keyword = url.searchParams.get('keyword') || ''
      
      return {
        code: 200,
        msg: '获取成功',
        data: {
          keyword,
          suggestions: Mock.mock({
            'list|5-10': [`${keyword}@ctitle(1, 3)`]
          }).list
        }
      }
    }
  }
]

export default searchMocks